﻿using System;
using System.Windows.Input;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.Foundation.Data;
using Omniscient.TimeTracker.BusinessModel;
using System.Windows.Media.Animation;

namespace Omniscient.TimeTracker.TaskBarMenu
{
    /// <summary>
    /// Interaction logic for Notifier.xaml
    /// </summary>
    public partial class Notifier
    {
        private ITimeEntryManager _manager;

        public Notifier(TaskBarMenuModel model)
        {
            InitializeComponent();

            NotificationIcon.MouseClick += NotificationIcon_MouseClick;
            NotificationIcon.ContextMenu = new NotifierContextMenu(model);

            // Set up manager
            _manager = ApplicationManager.Current.ObjectContainer.Get<ITimeEntryManager>();
            _manager.StateChanged += _manager_StateChanged;
        }

        private void NotificationIcon_MouseClick(object sender, MouseButtonEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Released)
            {
                ShowTrayBallonTip();
            }
        }

        private void _manager_StateChanged(object sender, EventArgs e)
        {
            Storyboard iconAnimation;

            if (_manager.HasRunningTask)
            {
                iconAnimation = Resources["StartTaskAnimation"] as Storyboard;
                if (iconAnimation != null)
                {
                    iconAnimation.Begin(this, true);
                }
            }
            else
            {
                iconAnimation = Resources["StopTaskAnimation"] as Storyboard;
                if (iconAnimation != null)
                {
                    iconAnimation.Begin(this, true);
                }
            }
            ShowTrayBallonTip();
        }

        private void ShowTrayBallonTip()
        {
            if (_manager.HasRunningTask)
            {
                NotificationIcon.BalloonTipTitle = _manager.CurrentRunningTimeEntry.Task.ParentProject.Name;
                if (_manager.CurrentRunningTimeEntry.Category == null ||
                    _manager.CurrentRunningTimeEntry.Category.Status == EntityStatus.NotLoadedYet)
                    NotificationIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", 
                                                                    _manager.CurrentRunningTimeEntry.Task.Name, 
                                                                    "None");
                else
                    NotificationIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", 
                                                                    _manager.CurrentRunningTimeEntry.Task.Name,
                                                                    _manager.CurrentRunningTimeEntry.Category.Name);
            }
            else
            {
                NotificationIcon.BalloonTipText = "No current running task.";
            }

            NotificationIcon.ShowBalloonTip(2000);
        }
    }
}
